<template>
  <div class="admin-login-container">
    <div class="login-wrapper">
      <!-- 左侧插图Banner -->
      <div class="banner-section">
        <div class="banner-content">
          <div class="logo-section">
            <div class="logo">
              <el-icon :size="40" color="#ffff">
                <Management />
              </el-icon>
              <span class="logo-text">OrangeHire</span>
            </div>
            <p class="subtitle">超级管理员控制台</p>
          </div>

          <div class="illustration">
            <div class="admin-icon">
              <el-icon :size="120" color="#ffff">
                <UserFilled />
              </el-icon>
            </div>
            <h2 class="welcome-title">管理员专属入口</h2>
            <p class="welcome-desc">统一管理平台企业与用户，监控运营数据</p>
          </div>

          <div class="features">
            <div class="feature-item">
              <el-icon color="#ffff">
                <OfficeBuilding />
              </el-icon>
              <span>企业管理</span>
            </div>
            <div class="feature-item">
              <el-icon color="#ffff">
                <User />
              </el-icon>
              <span>用户管理</span>
            </div>
            <div class="feature-item">
              <el-icon color="#ffff">
                <DataAnalysis />
              </el-icon>
              <span>数据分析</span>
            </div>
          </div>
        </div>
      </div>

      <!-- 右侧登录表单 -->
      <div class="form-section">
        <div class="form-container">
          <div class="form-header">
            <h1>管理员登录</h1>
            <p>请使用管理员账号登录系统</p>
          </div>

          <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" class="login-form" size="large">
            <el-form-item prop="email">
              <el-input v-model="loginForm.email" placeholder="请输入邮箱" prefix-icon="Message" clearable />
            </el-form-item>

            <el-form-item prop="password">
              <el-input v-model="loginForm.password" type="password" placeholder="请输入密码" prefix-icon="Lock"
                show-password clearable />
            </el-form-item>

            <el-form-item>
              <div style="width: 100%;display: flex;justify-content: space-between;">
                <div>
                  <el-checkbox v-model="loginForm.remember">
                    记住登录状态
                  </el-checkbox>
                </div>
                <div>
                  <el-text class="switch" type="primary" @click="switchLoginType">
                    <el-icon>
                      <Switch />
                    </el-icon>
                    切换至企业端
                  </el-text>
                </div>
              </div>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" class="login-btn" :loading="loading" @click="handleLogin">
                登录
              </el-button>
            </el-form-item>
          </el-form>

          <div class="form-footer">
            <el-link type="primary" :underline="false">
              忘记密码？
            </el-link>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, nextTick } from 'vue'
import { ElLink, ElLoading, ElMessage, ElNotification } from 'element-plus'
import { Management, UserFilled, OfficeBuilding, User, DataAnalysis } from '@element-plus/icons-vue'
import router from '@/router'

const loginFormRef = ref()
const loading = ref(false)

const loginForm = reactive({
  email: '',
  password: '',
  remember: false
})

const loginRules = {
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  try {
    await loginFormRef.value.validate()
    loading.value = true

    // TODO: 调用登录API
    setTimeout(() => {
      loading.value = false
      ElMessage.success('登录成功')
    }, 1000)
    router.push('/admin/dashboard')
  } catch (error) {
    console.error('表单验证失败:', error)
  }
}
const switchLoginType = async () => {
  // 1. 先显示加载动画
  const loadingInstance = ElLoading.service({
    fullscreen: true,
    text: '正在切换至企业端...'
  });
  // 2. 等待 500ms（模拟加载过程）todo 这里需要做一些状态变更以及相关退出登录的网络请求;
  await new Promise(resolve => setTimeout(resolve, 1000));
  // 3. 关闭加载动画
  await nextTick(() => {
    loadingInstance.close();
  })
  // 4. 跳转到企业端登录页
  router.push('/enterprise/login');
  ElNotification({
    title: 'Success',
    message: '已为您切换至企业端',
    type: 'success',
  })
}
</script>

<style scoped>
.admin-login-container {
  min-height: 100vh;
  background: #f5f7fa;
  display: flex;
  align-items: center;
  justify-content: center;
  /* padding: 20px; */
}

.login-wrapper {
  width: 100%;
  min-width: 1200px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  display: flex;
  min-height: 100vh;
}

.banner-section {
  flex: 1;
  /* fallback for old browsers */
  background: #396afc;
  /* Chrome 10-25, Safari 5.1-6 */
  background: -webkit-linear-gradient(to right, #2948ff, #396afc);
  /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
  background: linear-gradient(to right, #2948ff, #396afc);
  padding: 60px 40px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  color: white;
  position: relative;
}

.banner-section::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><circle cx="20" cy="20" r="2" fill="%23ffffff" opacity="0.1"/><circle cx="80" cy="40" r="1" fill="%23ffffff" opacity="0.1"/><circle cx="40" cy="80" r="1.5" fill="%23ffffff" opacity="0.1"/></svg>');
}

.banner-content {
  position: relative;
  z-index: 1;
}

.logo-section {
  margin-bottom: 40px;
}

.logo {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}

.logo-text {
  font-size: 28px;
  font-weight: bold;
  margin-left: 12px;
  color: white;
}

.subtitle {
  font-size: 16px;
  opacity: 0.9;
  margin: 0;
}

.illustration {
  text-align: center;
  margin-bottom: 40px;
}

.admin-icon {
  margin-bottom: 20px;
}

.welcome-title {
  font-size: 24px;
  margin-bottom: 12px;
  font-weight: 600;
}

.welcome-desc {
  font-size: 16px;
  opacity: 0.9;
  line-height: 1.5;
  margin: 0;
}

.features {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.feature-item {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 16px;
  opacity: 0.9;
}

.form-section {
  flex: 1;
  padding: 60px 40px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.form-container {
  width: 100%;
  max-width: 400px;
}

.form-header {
  text-align: center;
  margin-bottom: 40px;
}

.form-header h1 {
  font-size: 28px;
  color: #303133;
  margin-bottom: 8px;
  font-weight: 600;
}

.form-header p {
  color: #909399;
  font-size: 14px;
  margin: 0;
}

.login-form {
  margin-bottom: 20px;
}

.login-btn {
  width: 100%;
  height: 44px;
  font-size: 16px;
  background: #1890FF;
  border-color: #1890FF;
}

.login-btn:hover {
  background: #096DD9;
  border-color: #096DD9;
}

.form-footer {
  text-align: center;
}

@media (max-width: 768px) {
  .login-wrapper {
    flex-direction: column;
    max-width: 400px;
  }

  .banner-section {
    padding: 40px 20px;
  }

  .form-section {
    padding: 40px 20px;
  }
}

.switch {
  cursor: pointer;
  transition: all 0.2s;
}

.switch:hover {
  text-decoration: underline;
}
</style>
